package com.icim.xmtm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.xmtm.entity.CtSlimifshipbox;

@Repository
public class CtSlimifshipboxDao {
	@Resource
	private JdbcTemplate xmtmJdbcTemplate;

	public List<CtSlimifshipbox> queryNewBox(String vendorCode) {
		String sql = "SELECT FACTORYNAME,PRODUCTREQUESTNAME,PRODUCTSPECNAME,PRODUCTTYPE,THICKNESS,TARGETPRODUCTSPECNAME,"
				+ "TARGETPRODUCTREQUEST,TARGETTHICKNESS,LOTNAME,BOXNAME,BOXTYPE,BOXCAPACITY,PRODUCTQUANTITY,"
				+ "EVENTUSER,EVENTTIME,RELEASEQUANTITY,RECEIVESTATE,RECEIVEFLAG,DELIVERYTIME,ERRORMESSAGE "
				+ "FROM V_CT_SLIMIFSHIPBOX_WG WHERE RECEIVEFLAG = ?  AND vendorcode = ?";
		return xmtmJdbcTemplate.query(sql, new Object[] { "N",vendorCode },
				new RowMapper<CtSlimifshipbox>() {
					@Override
					public CtSlimifshipbox mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return setEntityByRs(rs);
					}

				});
	}

	public void updateBoxReceiveflag(String boxId, String receiveFlg,
			Timestamp deliveryTime, String errorMessage) {
		String sql = "UPDATE V_CT_SLIMIFSHIPBOX_WG SET RECEIVEFLAG = ? , DELIVERYTIME = ? , ERRORMESSAGE = ? WHERE BOXNAME = ? ";

		errorMessage = errorMessage.length() > 1000 ? errorMessage.substring(0,
				1000) : errorMessage;
		xmtmJdbcTemplate.update(sql, receiveFlg, deliveryTime, errorMessage,
				boxId);
	}


	public CtSlimifshipbox get(String boxName) {
		String sql = "SELECT * FROM V_CT_SLIMIFSHIPBOX_WG WHERE BOXNAME=?";
		List<CtSlimifshipbox> boxList = xmtmJdbcTemplate.query(sql,
				new Object[] { boxName }, new RowMapper<CtSlimifshipbox>() {

					@Override
					public CtSlimifshipbox mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return setEntityByRs(rs);
					}

				});
		return boxList.isEmpty() ? null : boxList.get(0);
	}

	private CtSlimifshipbox setEntityByRs(ResultSet rs) throws SQLException {
		CtSlimifshipbox cfSlimifshipbox = new CtSlimifshipbox();
		cfSlimifshipbox.setFactoryname(rs.getString("FACTORYNAME"));
		cfSlimifshipbox.setProductrequestname(rs
				.getString("PRODUCTREQUESTNAME"));
		cfSlimifshipbox.setProductspecname(rs.getString("PRODUCTSPECNAME"));
		cfSlimifshipbox.setProducttype(rs.getString("PRODUCTTYPE"));
		cfSlimifshipbox.setThickness(rs.getString("THICKNESS"));
		cfSlimifshipbox.setTargetproductspecname(rs
				.getString("TARGETPRODUCTSPECNAME"));
		cfSlimifshipbox.setTargetproductrequest(rs.getString("TARGETPRODUCTREQUEST"));
		cfSlimifshipbox.setTargetthickness(rs.getString("TARGETTHICKNESS"));
		cfSlimifshipbox.setLotname(rs.getString("LOTNAME"));
		cfSlimifshipbox.setBoxname(rs.getString("BOXNAME"));
		cfSlimifshipbox.setBoxtype(rs.getString("BOXTYPE"));
		cfSlimifshipbox.setBoxcapacity(rs.getInt("BOXCAPACITY"));
		cfSlimifshipbox.setProductquantity(rs.getInt("PRODUCTQUANTITY"));
		cfSlimifshipbox.setEventuser(rs.getString("EVENTUSER"));
		cfSlimifshipbox.setEventtime(rs.getTimestamp("EVENTTIME"));
		cfSlimifshipbox.setReleasequantity(rs.getInt("RELEASEQUANTITY"));
		cfSlimifshipbox.setReceivestate(rs.getString("RECEIVESTATE"));
		cfSlimifshipbox.setReceiveflag(rs.getString("RECEIVEFLAG"));
		cfSlimifshipbox.setDeliverytime(rs.getTimestamp("DELIVERYTIME"));
		cfSlimifshipbox.setErrormessage(rs.getString("ERRORMESSAGE"));
		return cfSlimifshipbox;
	}
}
